﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;

namespace OWA
{
    public partial class OwaForm : Form
    {
        private System.Timers.Timer _tmrRefresh = new System.Timers.Timer(10 * 60 * 1000);      // Refresh automatically each 10 minutes
        private bool _enterKeyAlreadySent = false;                                              // After write Login/Password, send Enter only one time

        /// <summary>
        /// Display form 
        /// </summary>
        public OwaForm()
        {
            InitializeComponent();

            this.AutoRefresh = true;
            _tmrRefresh.Elapsed += new System.Timers.ElapsedEventHandler(_tmrRefresh_Elapsed);

            Version ver = new Version(Application.ProductVersion);
            lblCopyright.Text = "v." + ver.ToString(2) + " - Created by Denis Voituron - 2010";

            // Read data in registry
            browser.Navigate(Configuration.Url);
            
        }

        /// <summary>
        /// Manage the downloaded document
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // Set Login/Password in HTML Logon Page
            if (Configuration.Version == "2003" && e.Url.ToString().Contains("/bin/auth/owalogon.asp"))
            {                                
                HtmlElement txtUsername = browser.Document.GetElementById("username");
                txtUsername.SetAttribute("value", Configuration.Login);

                HtmlElement txtPassword = browser.Document.GetElementById("password");
                txtPassword.SetAttribute("value", Configuration.Password);
                
                HtmlElement rdoTrusted = browser.Document.GetElementById("rdoTrusted");
                rdoTrusted.SetAttribute("checked", "True");

                if (_enterKeyAlreadySent == false)
                    SendKeys.Send("{ENTER}");
                _enterKeyAlreadySent = true;

            }
            else if (Configuration.Version == "2007" && e.Url.ToString().Contains("/OWA/auth/logon.aspx"))
            {                
                HtmlElement txtUsername = browser.Document.GetElementById("username");
                txtUsername.SetAttribute("value", Configuration.Login);

                HtmlElement txtPassword = browser.Document.GetElementById("password");
                txtPassword.SetAttribute("value", Configuration.Password);

                HtmlElement rdoTrusted = browser.Document.GetElementById("rdoPrvt");
                rdoTrusted.SetAttribute("checked", "True");

                if (_enterKeyAlreadySent == false)
                    SendKeys.Send("{ENTER}");
                _enterKeyAlreadySent = true;

            }
            else
            {
                _tmrRefresh.Start();
            }
        }

        /// <summary>
        /// Check if this is a new opening demand
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void browser_StartNewWindow(object sender, ExtendedWebBrowser2.BrowserExtendedNavigatingEventArgs e)
        {
            _tmrRefresh.Stop();
            OwaForm frm = new OwaForm();

            frm.AutoRefresh = false;
            frm.browser.Navigate(e.Url);
            frm.ShowInTaskbar = false;
            e.Cancel = true;

            if (Configuration.ModalForm)
                frm.ShowDialog();
            else
                frm.Show();

            _tmrRefresh.Start();
        }

        /// <summary>
        /// Display a progress bar
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void browser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            progress.Minimum = 0;
            progress.Maximum = Convert.ToInt32(e.MaximumProgress);
            progress.Value = Convert.ToInt32(e.CurrentProgress);
        }

        /// <summary>
        /// Change login/password (and options) in registry
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lnkLogin_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            LoginForm frmLogin = new LoginForm();
            frmLogin.ShowDialog();
        }

        /// <summary>
        /// Close the window when the browser ask it
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void browser_Quit(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Allow an auto-refresh
        /// </summary>
        public bool AutoRefresh
        {
            get;
            set;
        }

        /// <summary>
        /// Start a refresh (to keep a connection alive)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _tmrRefresh_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (AutoRefresh)
            {
                browser.Refresh();
            }
        }
        
    }
}
